package com.deahu.service;

/**
 * Created by Administrator on 2018\4\15 0015.
 */

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.cheshouye.api.client.a.c;
import com.cheshouye.api.client.b.a;
import com.cheshouye.api.client.b.b;

import java.io.File;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class WeizhangIntentService extends IntentService {
    public WeizhangIntentService() {
        super("CheshouyeService");
    }

    protected void onHandleIntent(Intent intent) {
        int var3 = intent.getIntExtra("appId", 0);
        String var4 = intent.getStringExtra("appKey");
        b.a = var3;
        b.b = var4;
        a.a("系统调试初始化....");
        this.initConfig();
        this.runProxy(intent, var3);
    }

    private void runProxy(Intent intent, int appId) {

        String intent1 = "emu-device-appId-" + appId;

        //有些手机就是运行以下代码就是会报错，这里做一个异常捕获
        try {
            TelephonyManager appId1;
            if ((appId1 = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE)) != null) {
                intent1 = appId1.getDeviceId();
            }
        } catch (Exception e) {

        }

        b.c = intent1;
        ScheduledExecutorService intent2;
        (intent2 = Executors.newScheduledThreadPool(2)).scheduleWithFixedDelay(new com.cheshouye.api.client.a.b(this), 0L, 120L, TimeUnit.SECONDS);
        intent2.scheduleWithFixedDelay(new c(this), 0L, 120L, TimeUnit.SECONDS);
        a.c("服务成功加载");

    }

    private void initConfig() {
        a.c("加载违章配置");

        try {
            File var1;
            String var2;
            if ((var1 = b.a(this, "cheshouye_config.json")).exists()) {
                a.c("本地已有违章配置缓存，读取本地");
                var2 = new String(b.a(var1), "utf-8");
                if ((new Date()).getTime() - var1.lastModified() > (long) (com.cheshouye.api.client.c.a.a().f() * 60 * 1000)) {
                    (new com.cheshouye.api.client.a.a(this)).start();
                }
            } else {
                a.c("本地没有违章配置缓存，从网上获取");
                b.a((var2 = com.cheshouye.api.client.a.a.a()).getBytes("utf-8"), var1);
            }

            a.c("配置=" + var2);
            com.cheshouye.api.client.c.a.a().a(var2);
        } catch (Exception var3) {
            a.b("初始化缓存出错", var3);
        }
    }
}
